<template>
  <div>

    <div class="card" style="margin-bottom: 10px;">
      <el-input prefix-icon="Search" style="width: 300px; margin-right: 10px" v-model="data.name" placeholder="请输入名称查询"></el-input>
      <el-button type="primary" plain round @click="Search">查询</el-button>
      <el-button type="info" plain round style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" plain round @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="data.tableData" style="width: 100%">
        <el-table-column prop="id" label="序号" width="70" />
        <el-table-column prop="name" label="商品名称" />
        <el-table-column prop="img" label="商品图片">
          <template v-slot="scope">
            <el-image v-if="scope.row.img" :src="scope.row.img" :preview-src-list="[scope.row.img]" preview-teleported style="width: 40px; height: 40px; border-radius: 5px"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="商品价格" />
        <el-table-column prop="supplierName" label="供应商" />
        <el-table-column prop="categoryName" label="商品分类" />
        <el-table-column prop="count" label="商品库存" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" :icon="Edit" circle @click="handleEdit(scope.row)"></el-button>
            <el-button type="danger" :icon="Delete" circle @click="del(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card" v-if="data.total">
      <el-pagination @current-change="changePage" background layout="prev, pager, next" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
    </div>

    <el-dialog width="35%" v-model="data.formVisible" title="商品信息" destroy-on-close>
      <el-form :model="data.form" :rules="rules" ref="formRef" label-width="100px" label-position="right" style="padding-right: 40px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="data.form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
              class="upload-demo"
              :action="'http://localhost:9090/files/upload'"
              list-type="picture"
              :on-success="handleImgUploadSuccess"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="data.form.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品分类" prop="categoryId">
          <el-select v-model="data.form.categoryId" placeholder="请选择商品分类" style="width: 100%">
            <el-option
                v-for="item in data.categoryData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="data.form.supplierId" placeholder="请选择供货商" style="width: 100%">
            <el-option
                v-for="item in data.supplierIdData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品库存" prop="count">
          <el-input v-model="data.form.count" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { reactive, ref } from "vue"
import request from '@/utils/request'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const data = reactive({
  tableData: [],
  categoryData: [],
  supplierIdData: [],
  total: 0,
  pageNum: 1,
  pageSize: 5,
  formVisible: false,
  form: {},
  name: null
})

const rules = reactive({
  name: [
    { required: true, message: '请输入商品名称', trigger: 'blur'},
  ],
  price: [
    { required: true, message: '请输入商品价格', trigger: 'blur'},
  ]
})

const formRef = ref()

const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}

const handleImgUploadSuccess = (res) => {
  data.form.img = res.data
}

const loadCategory = () => {
  request.get('/category/selectAll').then(res => {
    if (res.code === '200') {
      data.categoryData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 查询供货商信息
const loadSupplier = () => {
  request.get('/supplier/selectAll').then(res => {
    if (res.code === '200') {
      data.supplierIdData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const save = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (data.form.id) {
        // 编辑
        request.put('/goods/update', data.form).then(res => {
          if (res.code === '200') {
            load()
            ElMessage.success('更新成功')
            data.formVisible = false
          }
        })
      } else {
        // 新增
        request.post('/goods/add',data.form).then(res => {
          if (res.code === '200') {
            load()
            ElMessage.success('新增成功')
            data.formVisible = false
          } else {
            ElMessage.error(res.msg)
          }
        })
      }
    }
  })
}

const load = () => {
  request.get('/goods/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name
    }
  }).then(res => {
    if (res.code === '200') {
      data.tableData = res.data?.list || []
      data.total = res.data?.total || 0
    } else {
      ElMessage.error(res.msg)
    }
  })
}

loadCategory()
load()
loadSupplier()
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

const del = (id) => {
  ElMessageBox.confirm('删除数据后无法恢复，您确认删除吗？', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/goods/deleteById?id=' + id).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success("操作成功")
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(res => {})
}

const Search = () => {
  load()
}
const changePage = (pageNum) => {
  data.pageNum = pageNum
  load()
}

const reset = () => {
  data.username = null
  data.name = null
  load()
}
</script>